package test;


public class FindGridClosestToSquareCommand {
	
	class GridDimensions {
		
		public GridDimensions(int numOfRows, int numOfColumns) {
			this.numOfRows = numOfRows;
			this.numOfColumns = numOfColumns;
		}
		private int numOfRows;
		private int numOfColumns;
		public int getNumOfRows() {
			return numOfRows;
		}
		public int getNumOfColumns() {
			return numOfColumns;
		}
	}
	
	public static void main(String[] args) {
		
		for(int i = 1; i < 10; i++) {
			GridDimensions gridDimensions = new FindGridClosestToSquareCommand().execute(i);
			System.out.println("Number of items: " + i);
			System.out.println("Number of rows:" + gridDimensions.getNumOfRows());
			System.out.println("Number of columns: " + gridDimensions.getNumOfColumns());
			System.out.println("-------------------------------------");
		}
	}
	
	public GridDimensions execute(int numOfItems) {

		double numRowsDouble = Math.sqrt(numOfItems);
		int numRowsInt = (int) numRowsDouble;
		
		if(numRowsDouble == numRowsInt) {
			return new GridDimensions(numRowsInt, numRowsInt);
		}
		
		int numColumnsInt = numRowsInt;
		int count = 0;
		while(numColumnsInt * numRowsInt < numOfItems) {
			if(count++ % 2 == 0) {
				numRowsInt++;
			} else {
				numColumnsInt++;
			}
		}
		
		return new GridDimensions(numRowsInt, numColumnsInt);
	}

}
